<?php

class Public_IndexController extends YouNet_Controller_Action
{
    public function init() {
        parent::init();
        $this->_helper->layout->setLayout('layout_facebook');
    }
    
    public function preDispatch()
	{
		parent::preDispatch();
	}
    
    
    private function _getpageUrl($id_page, $params = null)
	{
		if (!empty($params))
		{
			$app_data = urlencode(json_encode($params));
			return $this->_social->getFanPageUrl($id_page, 'page') . '&app_data=' . $app_data;
		}
		else
		{
			return $this->_social->getFanPageUrl($id_page, 'page');
		}
	}
    
    public function likeAction()
    {
        $configs_db = YouNet_Util::getDbTable("configs");
        $yt_video_id = $this->_getParam('yt_video_id', null);
        $id_page = $this->_getParam('id_page', 0);
        $id_video = $this->_getParam('id_video', 0);
        $mode = $this->_getParam('mode', 0);
        
        $config = $configs_db->getUrl($this->_params['mode'], $this->_params['id_page'], $this->_params['id_video']);

        if (Core_Util::isFacebookRequest())
        {
            $this->view->is_request = true;
            
            $pagesTbl = new Core_Model_DbTable_Pages();
            $pages = $pagesTbl->getPageName($id_page);
            $page_name = $pages->page_name;

            if ($config != null)
            {
                if ($config['mode_page'] == 1)
                {
                    $info = $this->view->youtube()->getInfoVideo($config['url']);
                }
                else
                {
                    $info = $this->view->youtube()->getVideoInfoByChanneUrl($config['url'], $id_video);
                }

                

                $this->view->share_title = sprintf('%s : %s', $page_name, $info['title']);
                $this->view->info = $info;
            }
            
            if($mode == YOUTUBE_MODE_PLAYLIST)
            {
                $info = Core_YouTube::getVideoEntryInfoById($yt_video_id);
                $this->view->share_title = sprintf('%s : %s', $page_name, $info['title']);
                $this->view->info = $info;
            }

            $this->view->url = $this->_social->getShareUrl('/public/index/like/mode/' . $mode . 
                    '/id_page/' . $id_page . '/id_video/' . $id_video . '/yt_video_id/' . $yt_video_id);
        }
        else
        {
            $params = array();
            if(!empty($id_video)) $params['id_video'] = $id_video;
            if(!empty($yt_video_id)) $params['yt_video_id'] = $yt_video_id;
            if(!empty($mode)) $params['mode'] = $mode;
            
            YouNet_Util::parentRedirect($this->_getpageUrl($this->_params['id_page'], $params));
        }
    }
}